<?php
include_once("config.php");
if (isset($_SESSION['adminName'])){
	$isAdmin=true;
}else{
	$isAdmin=false;
}
if (isset($_POST['username']) && isset($_POST['userpassword']) && isset($_POST['useraddress']) && isset($_POST['useremail']) && isset($_POST['usermobile'])){
	$isInputValid=true;
}else{
	$isInputValid=false;
}
if ($isInputValid){
	$username=mysql_real_escape_string(stripcslashes($_POST['username']));
	$userpassword=mysql_real_escape_string(stripcslashes($_POST['userpassword']));
	$useraddress=mysql_real_escape_string(stripcslashes($_POST['useraddress']));
	$useremail=mysql_real_escape_string(stripcslashes($_POST['useremail']));
	$usermobile=mysql_real_escape_string(stripcslashes($_POST['usermobile']));
  
  if (!empty($useremail)){
	  $queryStr="INSERT INTO users VALUES('$username','$useraddress','$useremail','$usermobile','$userpassword')";
	  mysql_query($queryStr) or die("Error: ".mysql_error()."<br/>go back <a href='viewuser.php'>here</a>");
	  $isSuccessful=true;
  }else{
  	$isSuccessful=false;
  }
}else{
	$isSuccessful=false;
}
if ($isAdmin && $isSuccessful){
  header("location: viewuser.php");
}else if(!$isAdmin){
  header("location: adminIndex.php");
}else if(!$isInputValid){
	echo "Input is not valid!<br/> go back <a href='viewuser.php'>here</a>";
}else{
	echo "Input format does not fulfill requirements. <br/> go back <a href='viewuser.php'>here</a>";
}
?>
